290E - HQ - CodeForces Solution


*special problem constructive algorithms *2500

Please click on ads to support us..

Python Code:

a = input()
b = []
h = ''
c = 0
for i in a:
	if i == 'Q':
		c += 1
if c == 0:
	print('Yes')
	exit(0)
r = -1
for i in range(1001):
	if i*i == c:
		r = i
		break
if r == -1:
	print('No')
	exit(0)
h = [a.split('Q')[0], a.split('Q')[-1]]
c = [len(h[0]), len(h[1])]
if c[0] % 2 != 0 or c[1] % 2 != 0:
	print('No')
	exit(0)
c[0] //= 2
c[1] //= 2
resp = ''
i = c[0]
while True:
	if i >= len(a):
		break
	if r == 0 and a[i] == 'Q':
		break
	resp += a[i]
	if r == 0 and a[i] == 'H':
		c[1] -= 1
	if c[1] == 0 and r == 0:
		break
	if a[i] == 'Q':
		r -= 1
	if r == -1:
		print('No')
		exit(0)
	i += 1


def hq(a):
	resp = ''
	for i in a:
		if i == 'H':
			resp += 'H'
		else:
			resp += a
	return resp

if a == hq(resp):
	print('Yes')
else:
	print('No')


Comments

Submit
0 Comments
More Questions

630D - Hexagons
1690D - Black and White Stripe
1688D - The Enchanted Forest
1674C - Infinite Replacement
712A - Memory and Crow
1676C - Most Similar Words
1681A - Game with Cards
151C - Win or Freeze
1585A - Life of a Flower
1662A - Organizing SWERC
466C - Number of Ways
1146A - Love "A"
1618D - Array and Operations
1255A - Changing Volume
1710C - XOR Triangle
415C - Mashmokh and Numbers
8A - Train and Peter
591A - Wizards' Duel
1703G - Good Key Bad Key
1705A - Mark the Photographer
1707A - Doremy's IQ
1706B - Making Towers
1325B - CopyCopyCopyCopyCopy
1649C - Weird Sum
1324B - Yet Another Palindrome Problem
525A - Vitaliy and Pie
879A - Borya's Diagnosis
1672B - I love AAAB
1673A - Subtle Substring Subtraction
1345A - Puzzle Pieces